package com.loongtech.bi.action.player;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.LostPlayerManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 玩家分析-等级流失分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class LostLevelPlayerAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
    private Date date = new Date(Time.getBeginningOfDay(Time.currentTime() - 2 * Time.DAY));

    private String selectedType = "2";

    private List<LostPlayerManager.LossUserData> allUserLossDatas;
	private List<LostPlayerManager.LossUserData> dataList1;  // 前端要显示的数据1
	private ChartUIData chartData1;  // 前端要显示的柱状图1数据

    private List<LostPlayerManager.LossRoleSchoolData> dataList2;  // 前端要显示的数据2
    private ChartUIData chartData2;  // 前端要显示的柱状图2数据

    private List<LostPlayerManager.LossRoleSchoolMainData> dataList3;  // 前端要显示的数据3
    private ChartUIData chartData3;  // 前端要显示的柱状图3数据
	
	@Autowired
	private LostPlayerManager lostPlayerManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "查询玩家分析-流失分析");
		
        allUserLossDatas = lostPlayerManager.getLossUserDatas(date, queryPlatform, queryChannel, queryServer);
        selectType();

        dataList2 = lostPlayerManager.getLossRoleSchoolDatas(date, queryPlatform, queryChannel, queryServer);
        if (dataList2 != null && dataList2.size() > 0) {
            chartData2 = new ChartUIData();
            chartData2.setJsonType(ChartType.SPLINE.type);
            chartData2.setJsonStr(new ChartTool(dataList2, LostPlayerManager.LossRoleSchoolData.class, "").loadData(chartData2.getJsonType()));
        } else {
            chartData2 = null;
        }

        dataList3 = lostPlayerManager.getLossRoleSchoolMainDatas(date, queryPlatform, queryChannel, queryServer);
        if (dataList3 != null && dataList3.size() > 0) {
            chartData3 = new ChartUIData();
            chartData3.setJsonType(ChartType.SPLINE.type);
            chartData3.setJsonStr(new ChartTool(dataList3, LostPlayerManager.LossRoleSchoolMainData.class, "").loadData(chartData3.getJsonType()));
        } else {
            chartData3 = null;
        }
	}

    public void selectType() {
        dataList1 = new ArrayList<>();
        if (allUserLossDatas != null && allUserLossDatas.size() > 0) {
            for (LostPlayerManager.LossUserData d : allUserLossDatas) {
                if (selectedType.equals(d.getLossType())) {
                    dataList1.add(d);
                }
            }
        }
        if (dataList1.size() > 0) {
            chartData1 = new ChartUIData();
            chartData1.setJsonType(ChartType.SPLINE.type);
            chartData1.setJsonStr(new ChartTool(dataList1, LostPlayerManager.LossUserData.class, "").loadData(chartData1.getJsonType()));
        } else {
            chartData1 = null;
        }
    }
	
	@Override
	protected String csvContent() {
		return "流失分析";
	}

	@Override
    protected String csvFileName() {
	    return "流失分析";
    }

    public List<LostPlayerManager.LossUserData> getAllUserLossDatas() {
        return allUserLossDatas;
    }

    public void setAllUserLossDatas(List<LostPlayerManager.LossUserData> allUserLossDatas) {
        this.allUserLossDatas = allUserLossDatas;
    }

    public List<LostPlayerManager.LossUserData> getDataList1() {
        return dataList1;
    }

    public void setDataList1(List<LostPlayerManager.LossUserData> dataList1) {
        this.dataList1 = dataList1;
    }

    public ChartUIData getChartData1() {
		return chartData1;
	}

	public void setChartData1(ChartUIData chartData1) {
		this.chartData1 = chartData1;
	}

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getSelectedType() {
        return selectedType;
    }

    public void setSelectedType(String selectedType) {
        this.selectedType = selectedType;
    }

    public List<LostPlayerManager.LossRoleSchoolData> getDataList2() {
        return dataList2;
    }

    public void setDataList2(List<LostPlayerManager.LossRoleSchoolData> dataList2) {
        this.dataList2 = dataList2;
    }

    public ChartUIData getChartData2() {
        return chartData2;
    }

    public void setChartData2(ChartUIData chartData2) {
        this.chartData2 = chartData2;
    }

    public List<LostPlayerManager.LossRoleSchoolMainData> getDataList3() {
        return dataList3;
    }

    public void setDataList3(List<LostPlayerManager.LossRoleSchoolMainData> dataList3) {
        this.dataList3 = dataList3;
    }

    public ChartUIData getChartData3() {
        return chartData3;
    }

    public void setChartData3(ChartUIData chartData3) {
        this.chartData3 = chartData3;
    }
}
